package com.ch3;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * interrupt 打断正在运行的线程
 */
@Slf4j(topic = "c.Thread")
public class ThreadDemo_09 {

    public static void main(String[] args) throws InterruptedException {
        t1();
    }

    public static void t1() throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()){
                    log.debug("被打断了");
                    return;}
            }
        });
        log.debug("线程开始运行");
        thread.start();
        TimeUnit.SECONDS.sleep(5);
        log.debug("打断线程");
        thread.interrupt();
    }

}
